#!/usr/bin/python

#Ganna Raboshchuk {ganna.raboshchuk@upc.edu}
#Updated: 09.01.2015
#UPC, Barcelona

#Creation of the block-based hypothesis file:
# - the specific class is a parameter;
# - removed explicit scenarios iteration.
#--------------------------------------------------------------------------
import os.path
import math
import sys

#------------------------------------------------------------------------------
#Global variables
evaluation_interval = int(sys.argv[1]) #in seconds
path = sys.argv[2]
class_of_interest = sys.argv[3]

gmmOutputFolder = path.rsplit('/', 1)[0]
smoothedFilename = path.rsplit('/', 1)[-1]
session = smoothedFilename.split('_', 1)[0]

#Indexes of labels
START_TIME = 0
END_TIME = 1
CLASS = 2

filenames = []

#------------------------------------------------------------------------------
# Separation part.
if not os.path.isfile(path):
    sys.exit('File not found! ' + path)
else:
    inputFile = open(path, 'r')
    newpath = gmmOutputFolder + '/evaluation_interval_' + str(evaluation_interval) + '/' + session + '/'
    with open(path) as fileID:
        for line in fileID:
            if not line[0].isdigit():
                if (line != '#!MLF!#\n' and line != '.\n'):
                    filename = line.rsplit('/', 1)[-1]
                    filename = filename.split('.')[0] #session_scenario
                    filenames.append(filename)
                    outputFile = open(newpath + filename + '.txt', 'w')
            if (line == '.\n'):
                outputFile.close()
            if (line[0].isdigit()):
                interval = line.split(' ')
                outputFile.write(str(float(interval[START_TIME])/10000000) + ',' + str(float(interval[END_TIME])/10000000) + ',' + str(interval[CLASS]) + '\n')
inputFile.close()
outputFile.close()

# Label creating part.
outputFile2 = open(gmmOutputFolder + '/evaluation_interval_' + str(evaluation_interval) + '/' + 'overall_last_interval.txt', 'w')
for filename in filenames:
    path = gmmOutputFolder + '/evaluation_interval_' + str(evaluation_interval) + '/' + session + '/' + filename + '.txt'
    if not os.path.isfile(path):
        print 'File cannot be found! ' + path
        continue
    inputFile = open(path, 'r')
    outputFile = open(gmmOutputFolder + '/evaluation_interval_' + str(evaluation_interval) + '/' + session + '/' + filename + '.csv', 'w')
    with open(path) as fileID:
        interval_index = 1
        interval_counts = 0
        interval_partials = 0
        for line in fileID:
            if not line[0].isdigit():
                continue
            interval = line.split(',')
            interval_start = (interval_index - 1) * evaluation_interval
            interval_end = interval_index * evaluation_interval
            if class_of_interest in interval[CLASS]:
                if (float(interval[START_TIME]) == float(interval_end)):
                    if (interval_counts > 1 or interval_counts >= 1 and interval_partials > 0):
                        outputFile.write(str(interval_start) + ',' + str(interval_end) + ',' + class_of_interest + '\n')
                    else:
                        outputFile.write(str(interval_start) + ',' + str(interval_end) + ',' + 'ot' + '\n')
                    interval_index += 1
                    interval_counts = 0
                    interval_partials = 0
                if (float(interval[END_TIME]) > float(interval_end) and float(interval[START_TIME]) != float(interval_end)):
                    interval_partials += 1
                    if (interval_counts > 1 or interval_counts == 1 and interval_partials > 0):
                        outputFile.write(str(interval_start) + ',' + str(interval_end) + ',' + class_of_interest + '\n')
                    else:
                        outputFile.write(str(interval_start) + ',' + str(interval_end) + ',' + 'ot' + '\n')
                    interval_index += 1
                    interval_counts = 0
                    interval_partials = 1
                else:
                    interval_counts += 1
            else:
                previous_interval_index = interval_index
                if (float(interval[END_TIME]) > float(interval_end)):
                    interval_index = int(math.ceil((float(interval[END_TIME]) - evaluation_interval)/evaluation_interval)+1)
                if (float(interval[END_TIME]) == float(interval_end)):
                    interval_index = int(math.ceil((float(interval[END_TIME]) - evaluation_interval)/evaluation_interval)+2)
                if (float(interval[END_TIME]) >= float(interval_end) and interval_index - previous_interval_index >= 1):
                    for x in range (previous_interval_index, interval_index):
                        interval_start = (x - 1) * evaluation_interval
                        interval_end = x * evaluation_interval
                        if (interval_counts > 1 or interval_counts == 1 and interval_partials > 0):
                            outputFile.write(str(interval_start) + ',' + str(interval_end) + ',' + class_of_interest + '\n')
                        else:
                            outputFile.write(str(interval_start) + ',' + str(interval_end) + ',' + 'ot' + '\n')
                        interval_counts = 0
                        interval_partials = 0
        if (float(interval[END_TIME]) - float(interval_start) - float(evaluation_interval) < 0): #calculate last interval
            last_interval = str(float(interval[END_TIME]) - float(interval_start))
            if (interval_counts > 1 or interval_counts == 1 and interval_partials > 0):
                outputFile.write(str(interval_start) + ',' + str(interval[END_TIME]) + ',' + class_of_interest + '\n')
            else:
                outputFile.write(str(interval_start) + ',' + str(interval[END_TIME]) + ',' + 'ot' + '\n')
        else:
            last_interval = str(float(interval[END_TIME]) - float(interval_start) - float(evaluation_interval))
            if (interval_counts > 1 or interval_counts == 1 and interval_partials > 0):
                outputFile.write(str(interval_start + evaluation_interval) + ',' + str(interval[END_TIME]) + ',' + class_of_interest + '\n')
            else:
                outputFile.write(str(interval_start + evaluation_interval) + ',' + str(interval[END_TIME]) + ',' + 'ot' + '\n')
        outputFile2.write(last_interval + '\n')
        interval_index = 1
        interval_counts = 0
        interval_partials = 0
inputFile.close()
outputFile.close()
outputFile2.close()
